<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">用户管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a><cite>系统管理</cite></a>
          <a><cite>用户管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <!-- 搜索条件 -->
        <div class="layui-form toolbar">
            <form id="searchForm" onsubmit="return false;">
                账号：<input name="username" class="layui-input search-input" type="text" placeholder="输入账号"/>&emsp;
                用户名：<input name="realName" class="layui-input search-input" type="text" placeholder="输入用户名"/>&emsp;
                状态：<select name="status" lay-verify="">
                <option value="">请选择一个状态</option>
                <option value="0">正常</option>
                <option value="-1">禁用</option>
            </select>
                <button id="user-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            </form>
        </div>
        <!-- 表格顶部操作列 -->
        <script type="text/html" id="user-toolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add"><i
                        class="layui-icon">&#xe654;</i>添加
                </button>
                <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="import"><i class="layui-icon">&#xe656;</i>导入
                </button>
                <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="operate"><i class="layui-icon">&#xe63c;</i>操作日志
                </button>
            </div>
        </script>
        <table class="layui-table" id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="user-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="reset">重置密码</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="user-tpl-status">
    <input type="checkbox" lay-filter="user-tpl-status" value="{{d.id}}" lay-skin="switch"
           lay-text="正常|禁用" {{d.status==0?'checked':''}}/>
</script>

<script>
    layui.use(['form', 'table', 'config', 'crab'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var crab = layui.crab;

        // 渲染表格
        var userTable = table.render({
            elem: '#user-table',
            toolbar: '#user-toolbar',
            url: config.base_server + '/sys/user/page',
            parseData: config.parseData,
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'username', title: '账号', width: 120},
                {field: 'nickName', title: '昵称', width: 120},
                {field: 'realName', title: '真实姓名', width: 120},
                {field: 'phone', title: '手机号', width: 120},
                {field: 'email', title: '电子邮箱'},
                {field: 'sex', sort: true, title: '性别', width: 80},
                {field: 'status', templet: '#user-tpl-status', title: '状态', width: 100},
                {field: 'createTime', title: '创建时间', sort: true, width: 150},
                {fixed: 'right', align: 'center', toolbar: '#user-table-bar', title: '操作', width: 180}
            ]]
        });
        // 表格顶部操作列
        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                showEditModel();
            }
        });
        // 表格操作列事件
        table.on('tool(user-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') {
                // 修改
                showEditModel(data);
            } else if (obj.event === 'reset') {
                // 删除
                layer.confirm('确定要重置该条记录的用户登录密码吗？', function () {
                    crab.put('/sys/user/reset_password/' + obj.data.id, {}, function () {
                        crab.msg('登录密码重置成功！')
                    });
                });
            } else if (obj.event === 'del') {
                // 删除
                layer.confirm('确定要删除该条用户记录吗？', function () {
                    crab.deleteRow(obj, '/sys/user/' + obj.data.id);
                });
            }
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            crab.putFormData(data);
            crab.popupCenter({
                title: data ? '修改用户' : '添加用户',
                path: '/components/sys/user-form.html',
                finish: function () {
                    userTable.reload('user-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        $('#user-btn-search').click(function () {
            userTable.reload({where: crab.getSearchForm()});
        });

        // 修改user状态
        form.on('switch(user-tpl-status)', function (obj) {
            crab.put('/sys/user/status_' + obj.elem.value, {
                status: obj.elem.checked ? 0 : -1
            }, function () {
                layer.msg('用户状态修改成功', {icon: 1});
            });
        });
    });
</script>